package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletConch;
import org.itboys.mobile.service.mysql.web.wallet.SundayWalletService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_钱包_后台_controller
 */
@Controller
@RequestMapping("/sunday/web/wallet")
public class SundayWalletController extends BaseController {
    @Autowired
    private SundayWalletService walletService;


    /**
     * 订单模块主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/conch/index")
    public String code_index(
            HttpServletRequest request,
            Model model
            ){
        return "/sunday/wallet/conch/index";
    }

    /**
     * 获取充值码
     * @param request
     * @param response
     * @param status
     */
    @RequestMapping("/conch/select")
    public void conch_select(HttpServletRequest request,HttpServletResponse response,
                       @RequestParam(value="status",required=true) Integer status){
        try {
            PageResult<SundayWalletConch> result = walletService.selectCode(request,status);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/wallet/code/select------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }


    /**
     * 批量生产充值码
     * @param request
     * @param response
     * @param num
     * @param amount
     */
    @RequestMapping("/conch/generate")
    public void conch_generate(HttpServletRequest request,
                     HttpServletResponse response,
                            @RequestParam(value="num",required=true) Integer num,
                            @RequestParam(value="amount",required=true)BigDecimal amount
    ) {
        try {
            walletService.generateCode(num,amount);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/wallet/conch/generate------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, msg);
        }
    }


}
